package com.iyao_app.mvp.preview.fragment;

import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.image.QualityInfo;
import com.iyao_app.R;
import com.iyao_app.base.BaseFragment;
import com.iyao_app.preview.OnPhotoTapListener;
import com.iyao_app.preview.PhotoDraweeView;
import com.iyao_app.util.Contants;
import com.iyaowoqu.base_library.util.LogUtil;
import com.iyaowoqu.base_library.util.ScreenUtils;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/12/12.
 */

public class ImagePreviewFragment extends BaseFragment {
    PhotoDraweeView mPhotoDraweeView;
    TextView mTvSize;
    LinearLayout mLinDown;

    private int mWidth = 0;
    private int mHeight = 0;

    private ImageInfo mImageInfo;
    private String image;

    @Override
    public View initRootView(LayoutInflater inflater) {
        rootView = mInflater.inflate(R.layout.activity_image_view, null);
        return rootView;
    }

    @Override
    public void setViews() {
        if (getArguments() == null || getArguments().getString(Contants.TYPE) == null) {
            return;
        }
        image = getArguments().getString(Contants.TYPE);
        mPhotoDraweeView = rootView.findViewById(R.id.photoView);
        mTvSize = rootView.findViewById(R.id.tv_size);
        mLinDown = rootView.findViewById(R.id.lin_down);

        if (!TextUtils.isEmpty(image)) {
            mWidth = ScreenUtils.getScreenWidth();

            mPhotoDraweeView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//            mPhotoDraweeView.setImageURI(imageUrl);
            //添加点击事件
            mPhotoDraweeView.setOnPhotoTapListener(new OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {
                    finish();
                }
            });
            if (!TextUtils.isEmpty(image)) {


                ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
                    @Override
                    public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
                        if (imageInfo == null || mPhotoDraweeView == null) {
                            return;
                        }
                        mImageInfo = imageInfo;
                        QualityInfo qualityInfo = imageInfo.getQualityInfo();
                        LogUtil.e("==qualityInfo==" + qualityInfo.getQuality());
                        mHeight = (int) ((float) (mWidth * imageInfo.getHeight()) / (float) imageInfo.getWidth());
                        mPhotoDraweeView.update(imageInfo.getWidth(), imageInfo.getHeight());
                    }

                    @Override
                    public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {

                    }

                    @Override
                    public void onFailure(String id, Throwable throwable) {
                        throwable.printStackTrace();
                    }
                };

                try {

                    PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                            .setOldController(mPhotoDraweeView.getController())
                            .setUri(Uri.parse(image))
                            .setControllerListener(controllerListener)
                            .build();
                    mPhotoDraweeView.setController(controller);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }

    }

    @Override
    public void initData() {

    }
}
